import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { useCounterStore } from '@/stores/counter'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
      
    },
    {
      path: '/user',
      name: 'user',
      component: () => import('../views/UserView.vue'),
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/card',
      name: 'card',
      component: () => import('../views/CardView.vue'),
      meta: {
        requireAuth: true,
        isTab: false
      }
    },
    
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
      meta: {
        isTab: false
      }
    },
    {
      path: '/order',
      name: 'order',
      component: () => import('../views/OrderView.vue'),
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/ResgiterView.vue'),
      meta: {
        isTab: false
      }
    },
    {
      // 第一种
      // path: '/foods',
      // 第二种动态路由传参
      path: '/foods/:shopId',
      name: 'foods',
      component: () => import('../views/FoodsView.vue'),
      meta: {
        isTab: false
      }
    },
    {
      path: '/detail',
      name: 'detail',
      component: () => import('../views/DetailView.vue'),
      meta: {
        isTab: false
      }
    },
    {
      path: '/buy1',
      name: 'buy1',
      component: () => import('../views/BuyView1.vue'),
      meta: {
        isTab: false,
        requireAuth: true
      }
    },
    {
      path: '/buy2',
      name: 'buy2',
      component: () => import('../views/BuyView2.vue'),
      meta: {
        isTab: false,
        requireAuth: true
      }
    },
    {
      path: '/comment',
      name: 'comment',
      component: () => import('../views/Comment.vue'),
      meta: {
        isTab: false,
        requireAuth: true
      }
    }
    ,
    {
      path: '/collect',
      name: 'collect',
      component: () => import('../views/CollectView.vue'),
      meta: {
        isTab: false,
        requireAuth: true
      }
    },
    {
      path: '/search',
      name: 'search',
      component: () => import('../views/SearchView.vue'),
      meta: {
        isTab: false
      }
    }
    ,
    {
      path: '/updateuser',
      name: 'updateuser',
      component: () => import('../views/UpdateUserView.vue'),
      meta: {
        isTab: false,
        requireAuth: true
      }
    }
  ]
})

router.beforeEach((to,from,next)=>{
  const store = useCounterStore();
  const token = store.userId;
  if(to.meta.requireAuth){
    if(token){
      next();
    }
    else{
      next({name: "login"});
    }
  }
  else{
    next();
  }
});

export default router
